package com.atguigu.yygh.user.service.impl;

import com.atguigu.yygh.client.DictFeignClient;
import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.mapper.PatientMapper;
import com.atguigu.yygh.user.service.PatientService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zr
 * @date 2021/12/18 21:52
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {
    @Resource
    private DictFeignClient dictFeignClient;

    /**
     * 获取就诊人列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<Patient> findAllUserId(Long userId) {
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<Patient> patientList = baseMapper.selectList(queryWrapper);
        // 封装Patient其他参数
        patientList.stream().forEach(patient -> {
            this.packPatient(patient);
        });
        return patientList;
    }

    /**
     * 根据id获取就诊人信息
     *
     * @param id
     * @return
     */
    @Override
    public Patient getPatient(Long id) {
        Patient patient = this.packPatient(baseMapper.selectById(id));
        return patient;
    }

    /**
     * 远程调用 封装Patient其他参数
     *
     * @param patient
     */
    private Patient packPatient(Patient patient) {
        // 根据证件类型编码 获取证件类型具体值
        String certificatesType = dictFeignClient.getDictName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());
        // 联系人证件类型
        String contactsCertificatesType = dictFeignClient.getDictName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getContactsCertificatesType());
        //省
        String provinceString = dictFeignClient.getDictName(patient.getProvinceCode());
        //市
        String cityString = dictFeignClient.getDictName(patient.getCityCode());
        //区
        String districtString = dictFeignClient.getDictName(patient.getDistrictCode());
        patient.getParam().put("certificatesTypeString", certificatesType);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesType);
        patient.getParam().put("provinceString", provinceString);
        patient.getParam().put("cityString", cityString);
        patient.getParam().put("districtString", districtString);
        patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());
        return patient;
    }
}
